#include "VoxelGridModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.voxel_grid_model.json";
}

VoxelGridModel::VoxelGridModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/voxel_grid.html");
}

VoxelGridModel& VoxelGridModel::get()
{
    static VoxelGridModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void VoxelGridModel::fromJson(const QJsonObject& json, QStringList& errorList)
{

    BasePclModel::fromJson(json, errorList);

    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    leafSizeForX_ = json["leafSizeForX"].toDouble(leafSizeForX_);
    leafSizeForY_ = json["leafSizeForY"].toDouble(leafSizeForY_);
    leafSizeForZ_ = json["leafSizeForZ"].toDouble(leafSizeForZ_);
}

QJsonObject VoxelGridModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("leafSizeForX", QJsonValue(leafSizeForX_));
    json.insert("leafSizeForY", QJsonValue(leafSizeForY_));
    json.insert("leafSizeForZ", QJsonValue(leafSizeForZ_));

    return json;
}
